#  $%BEGINLICENSE%$
#  Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
# 
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; version 2 of the
#  License.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
#  02110-1301  USA
# 
#  $%ENDLICENSE%$

INCLUDE_DIRECTORIES(${GLIB_INCLUDE_DIRS})
LINK_DIRECTORIES(${GLIB_LIBRARY_DIRS})
LINK_DIRECTORIES(${LIBINTL_LIBRARY_DIRS})

INCLUDE_DIRECTORIES(${LUA_INCLUDE_DIRS})
LINK_DIRECTORIES(${LUA_LIBRARY_DIRS})

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/)
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR}) # for config.h

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) ## for the generated header file
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) ## for the packaged header file

#lemon bin
add_executable(lemon ${CETUS_TOOLS_DIR}/lemon.c)

#option(SIMPLE_PARSER "use the simple parser")
if(SIMPLE_PARSER)
  message("** Using simple parser [-DSIMPLE_PARSER=ON]")
  set(LEMON_GRAMMAR_FILE "${CMAKE_CURRENT_SOURCE_DIR}/simple-parser.y")
  set(LEMON_RAW_REPORT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/simple-parser.out")
  add_custom_command(TARGET lemon PRE_BUILD COMMAND echo "@@@ USING SIMPLE PARSER [-DSIMPLE_PARSER=ON]")
else(SIMPLE_PARSER)
  message("** Using full parser [-DSIMPLE_PARSER=OFF(default)]")
  set(LEMON_GRAMMAR_FILE "${CMAKE_CURRENT_SOURCE_DIR}/myparser.y")
  set(LEMON_RAW_REPORT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/myparser.out")
  add_custom_command(TARGET lemon PRE_BUILD COMMAND echo "@@@ USING FULL PARSER [-DSIMPLE_PARSER=OFF]")
endif(SIMPLE_PARSER)

#lemon parse
set(LEMON_PARSER_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/myparser.y.c)
set(LEMON_OUTPUT_HEADER ${CMAKE_CURRENT_BINARY_DIR}/myparser.y.h)
set(LEMON_TEMPATE_FILE ${CETUS_TOOLS_DIR}/lempar.c)

add_custom_command(
  OUTPUT  ${LEMON_PARSER_OUTPUT} ${SQL_TOKENS_HEADER}
  DEPENDS ${LEMON_GRAMMAR_FILE} ${LEMON_TEMPATE_FILE}
          ${CMAKE_CURRENT_SOURCE_DIR}/extra_tokens.inc
  COMMAND lemon -s -T${LEMON_TEMPATE_FILE} ${LEMON_GRAMMAR_FILE} -o${LEMON_PARSER_OUTPUT} -h${LEMON_OUTPUT_HEADER}

  COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/extra_tokens.inc >> ${LEMON_OUTPUT_HEADER}
  )

set_source_files_properties(${LEMON_PARSER_OUTPUT}  PROPERTIES GENERATED 1)
set_source_files_properties(${LEMON_OUTPUT_HEADER}  PROPERTIES GENERATED 1)

#flex -- depends on parse.h generated by lemon
find_package(FLEX)
set(FLEX_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mylexer.l.c)
FLEX_TARGET(MyLexer mylexer.l ${FLEX_OUTPUT} COMPILE_FLAGS
  "--header-file=${CMAKE_CURRENT_BINARY_DIR}/mylexer.l.h")
set_source_files_properties(${FLEX_OUTPUT}
  PROPERTIES GENERATED 1)

set(SQL_PARSER_SOURCES
    sql-expression.c
    sql-operation.c
    sql-property.c
    sql-context.c
    sql-construction.c
    sql-filter-variables.c
    ${FLEX_MyLexer_OUTPUTS}
    ${LEMON_PARSER_OUTPUT}
)

ADD_LIBRARY(sqlparser SHARED ${SQL_PARSER_SOURCES})

add_dependencies(sqlparser lemon)

CHASSIS_INSTALL_TARGET(sqlparser)
